﻿/* ============================================================================== 
* 功能描述：MaterialHelper 
* 创 建 者：张教主 
* 当前版本: V1.0
* 创建日期：2019/2/21 16:53:11 
* ==============================================================================*/

using Newtonsoft.Json;
using src.Domain;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace src.Helper
{
    /// <summary>
    /// 素材Helper
    /// </summary>
    public class MaterialHelper
    {
        #region 上传
        /// <summary>
        /// 上传临时素材
        /// </summary>
        /// <param name="token">accesstoken</param>
        /// <param name="filePath">附件路径</param>
        /// <param name="mediaType">素材类型 </param>
        /// <returns></returns>
        public static string UploadMedia(string token, string filePath, string mediaType)
        {
            if (!File.Exists(filePath)) throw new IOException("this file is not exist");
            bool b = Enum.TryParse<MediaType>(mediaType, out MediaType media);
            if (!b) throw new Exception("please input correct mediatype!");
            string postUrl = WCDevUrl.GetMediaUploadUrl(token, mediaType);
            HttpHelper httpHelper = new HttpHelper();
            string jsonResponse = httpHelper.PostFile(postUrl, filePath);
            return jsonResponse;
        }

        /// <summary>
        /// 上传临时素材
        /// </summary>
        /// <param name="token">accesstoken</param>
        /// <param name="filePath">附件路径</param>
        /// <param name="mediaType">素材类型 </param>
        /// <returns></returns>
        public static string UploadMedia(string token, string filePath, MediaType mediaType)
        {
            string jsonResponse = UploadMedia(token, filePath, mediaType.ToString());
            return jsonResponse;
        }
        #endregion

        /// <summary>
        /// 获取临时素材
        /// </summary>
        /// <param name="token">accesstoken</param>
        /// <param name="mediaId">素材id</param>
        /// <returns></returns>
        public static string GetMedia(string token,string mediaId)
        {
            string getUrl = WCDevUrl.GetMediaUrl(token,mediaId);
            HttpHelper httpHelper = new HttpHelper();
            string jsonResponse = httpHelper.GetStr(getUrl);
            return jsonResponse;
        }

        /// <summary>
        /// 新增永久图文素材
        /// </summary>
        /// <param name="token">accesstoken</param>
        /// <param name="data">图文信息</param>
        /// <returns></returns>
        public static string AddNewMaterial(string token, ArticleRoot data)
        {
            string postUrl = WCDevUrl.GetAddNewMediaUrl(token);
            HttpHelper httpHelper = new HttpHelper();
            string postStr = JsonConvert.SerializeObject(data);
            string jsonResponse = httpHelper.PostStr(postStr,postUrl);
            return jsonResponse;
        }

        /// <summary>
        /// <param name="token">accesstoken</param>
        /// </summary>
        /// <param name="token"></param>
        /// <param name="filePath">附件路径 图片仅支持jpg/png格式，大小必须在1MB以下。</param>
        /// <returns></returns>
        public static string UploadMaterialImg(string token,string filePath)
        {
            if (!File.Exists(filePath)) throw new Exception("please input correct filepath");
            string postUrl = WCDevUrl.GetUploadMaterialImgUrl(token);
            HttpHelper httpHelper = new HttpHelper();
            string jsonResponse = httpHelper.PostFile(postUrl, filePath);
            return jsonResponse;
        }
    }
}
